package com.rfid.emergencywarehouse.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.base.base.BaseAdapter;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.entity.NameValueBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectAdapter extends BaseAdapter<SelectAdapter.MyHolder,NameValueBean> {
    int mSelect = 0;

    public SelectAdapter(List<NameValueBean> data) {
        super(data);
        disableClick = true;
    }

    @Override
    protected int getLayout() {
        return R.layout.item_select;
    }

    @Override
    protected MyHolder getViewHolder(View view) {
        return new MyHolder(view);
    }

    @Override
    protected void bindView(MyHolder holder, int position, NameValueBean t) {
        holder.title.setText(mData.get(position).toString());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSelect = position;
                if (mListener != null) {
                    mListener.onClick(mData.get(position));
                }
                notifyDataSetChanged();
            }
        });
        if (mSelect == position) {
            holder.selectIcon.setImageResource(R.mipmap.checked);
        } else {
            holder.selectIcon.setImageResource(0);
        }
    }

    public NameValueBean getSelected() {
        if (mData.size() > 0)
            return mData.get(mSelect);
        return null;
    }

    class MyHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.select_icon)
        ImageView selectIcon;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
